package com.yifeng.repo.controller.excel.loader.action;

import com.yifeng.repo.base.utils.converter.JacksonHelper;
import com.yifeng.repo.controller.excel.configure.ExcelLoaderProperties;
import com.yifeng.repo.controller.excel.loader.biz.BizExportAction;
import com.yifeng.repo.controller.excel.loader.dto.DataTaskConf;
import com.yifeng.repo.controller.excel.loader.dto.DataTaskDto;
import com.yifeng.repo.controller.excel.loader.dto.RowDataDto;

import java.util.*;

/**
 * Created by daibing on 2024/4/18.
 */
public class GoodsExportAction implements BizExportAction {
    private int rowNumber = 0;

    @Override
    public DataTaskConf getTaskConf(ExcelLoaderProperties properties) {
        return new DataTaskConf(
                properties.getBatchSize(),
                properties.getQueueSize(),
                properties.getImportExcelDownTaskNumber(),
                properties.getImportLogBaseHeader(),
                properties.isImportLogFileSplit(),
                true
        );
    }

    @Override
    public String templateUrl() {
//                return "https://open.yifengx.com/templates/gcs-label/集团级标签导出模板.xlsx";
        return "https://snap.yifengx.com/YFX1326BqMdXsHyu/fire-dict/excel/template/集团级标签导出模板-多sheet.xlsx";
    }

    @Override
    public Map<Integer, String> templateHeader(int sheetNo) {
        Map<Integer, String> header = new LinkedHashMap<>();
        header.put(0, "goods_code");
        header.put(1, "bar_code");
        header.put(2, "goods_desc");
        header.put(3, "category_level_1");
        header.put(4, "category_level_2");
        header.put(5, "category_level_3");
        header.put(6, "category_level_4");
        header.put(7, "tag_key");
        header.put(8, "tag_value");
        header.put(9, "tag_modify_time");
        return header;
    }

    @Override
    public List<RowDataDto> source(int sheetNo, String offsetId, int size) {
        if (rowNumber > 100) {
            return Collections.emptyList();
//            return null;
        }
        return Arrays.asList(
                buildRowDataDto(rowNumber / 50, rowNumber++, "1006468\t1190582\t盐酸普罗帕酮片(心律平片) 50毫克*50片 江苏云阳集团药业有限公司\t心脑血管用药\t心脏病用药\t盐酸普罗帕酮\t盐酸普罗帕酮\t是否自有品牌\t否\t2023-03-30 00:00:00\n"),
                buildRowDataDto(rowNumber / 50, rowNumber++, "6032727\t6935616716813\t暖宝宝 1个 九州康三九生物科技(湖北)有限公司\t家居用品\t家居产品\t季节类用品\t季节类用品\t是否自有品牌\t否\t2023-03-30 00:00:00\n"),
                buildRowDataDto(rowNumber / 50, rowNumber++, "2021235\t2021235\t龙眼肉 10克*1袋\t普通饮片散装类\t散装类\t龙眼肉\t龙眼肉\t是否自有品牌\t否\t2023-03-30 00:00:00\n"),
                buildRowDataDto(rowNumber / 50, rowNumber++, "2152042\t2152042\t青葙子(药都堂) 10克\t普通饮片散装类\t散装类\t其他饮片\t其他饮片\t是否自有品牌\t否\t2023-03-30 00:00:00\n"),
                buildRowDataDto(rowNumber / 50, rowNumber++, "6018584\t6922464901539\t美赞臣限量版套装(爽身粉+洗发沐浴露) 160克+260毫升 广州诗妍贸易有限公\t母婴护理\t洗护用品\t沐浴露\t沐浴露\t是否自有品牌\t否\t2023-03-30 00:00:00\n"),
                buildRowDataDto(rowNumber / 50, rowNumber++, "2044609\t2044609\t藁本(三越) 10克\t普通饮片散装类\t散装类\t其他饮片\t其他饮片\t是否自有品牌\t否\t2023-03-30 00:00:00\n"),
                buildRowDataDto(rowNumber / 50, rowNumber++, "5003492\t6932593032745\t稳健医用纱布绷带(盒装) 10厘米*5米 稳健实业(深圳)有限公司\t消耗性卫生材料\t普通型一次性耗材\t绷带类\t绷带类\t是否自有品牌\t否\t2023-03-30 00:00:00\n"),
                buildRowDataDto(rowNumber / 50, rowNumber++, "1067990\t6925526209536\t穿心莲内酯片 (惠普森) 50毫克*36片 海南惠普森医药生物技术有限公司\t清热解毒用药\t清热解毒类\t穿心莲内酯\t穿心莲内酯\t是否自有品牌\t否\t2023-03-30 00:00:00\n"),
                buildRowDataDto(rowNumber / 50, rowNumber++, "3010191\t6959607508515\t罗汉果蜜炼枇杷糖 20粒 三九企业集团兰考生物制品有限公司\t一般保健品\t清咽润喉\t罗汉果类\t罗汉果类\t是否自有品牌\t否\t2023-03-30 00:00:00\n"),
                buildRowDataDto(rowNumber / 50, rowNumber++, "3003419\t6941678702688\t健佰牌鱼油维E软胶囊 (柏维力) 1.2克*100粒 宣城柏维力生物工程有限公司\t功能调节类\t辅助降脂、血糖\t鱼油类\t鱼油类\t是否自有品牌\t否\t2023-03-30 00:00:00\n")
        );
    }

    @Override
    public void totalFinished(DataTaskDto dataTaskDto) {
        System.out.println("total finished return: " + JacksonHelper.toJson(dataTaskDto));
        System.exit(0);
    }

    @Override
    public void failed(DataTaskDto dataTaskDto) {
        System.out.println("failed return: " + JacksonHelper.toJson(dataTaskDto));
        System.exit(-1);
    }

    private RowDataDto buildRowDataDto(int sheetNo, int rowNumber, String raw) {
        String[] split = raw.split("\t|\n");
        Map<String, String> data = new LinkedHashMap<>();
        data.put("id", String.valueOf(rowNumber));
        data.put("goods_code", split[0]);
        data.put("bar_code", split[1]);
        data.put("goods_desc", split[2]);
        data.put("category_level_1", split[3]);
        data.put("category_level_2", split[4]);
        data.put("category_level_3", split[5]);
        data.put("category_level_4", split[6]);
        data.put("tag_key", split[7]);
        data.put("tag_value", split[8]);
        data.put("tag_modify_time", split[9]);
        return new RowDataDto(sheetNo, rowNumber, data);
    }
}
